//package com.ftf.tp.blog.domain.article.task;
//
//import com.ftf.tp.blog.domain.article.repository.repository.pos.ArticleDataPO;
//import com.ftf.tp.blog.domain.article.repository.repository.pos.ArticleSupportPO;
//import com.ftf.tp.blog.infrastructurea.mapper.ArticleDataMapper;
//import com.ftf.tp.blog.infrastructurea.mapper.ArticleSupportMapper;
//import com.ftf.tp.common.utils.RedissonHelper;
//import RedisConstant;
//import DateUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.util.*;
//
///**
// * @author tanghao
// * @date 2021/12/1
// */
//@Slf4j
//@Component
//public class SupportTask {
//
//    @Resource
//    private RedissonHelper redissonHelper;
//
//    @Resource
//    private ArticleSupportMapper articleSupportMapper;
//
//    @Resource
//    private ArticleDataMapper articleDataMapper;
//
//    /**
//     * 文章点赞相关定时任务  2小时执行一次
//     */
//    @Scheduled(cron = "0 0 0/2 * * ?")
//    public void supportBlog() {
//        log.info("消费文章点赞缓存定时任务开启");
//        Set<Map.Entry<Object, Object>> entries = redissonHelper.hashGetAll(RedisConstant.REDIS_KEY_ARTICLE_SUPPORT);
//        Iterator<Map.Entry<Object, Object>> iterator = entries.iterator();
//        List<ArticleSupportPO> supportDOList = new ArrayList<>();
//        List<ArticleDataPO> articleDataPOList = new ArrayList<>();
//        //消费点赞记录
//        while (iterator.hasNext()) {
//            Map.Entry<Object, Object> next = iterator.next();
//            String key = (String) next.getKey();
//            String value = (String) next.getValue();
//            String[] split = key.split("::");
//            String userId = split[0];
//            String targetId = split[1];
//            String supportTime = split[2];
//            ArticleSupportPO supportDO = ArticleSupportPO.builder()
//                    .userId(Long.valueOf(userId))
//                    .articleId(Long.valueOf(targetId))
//                    .createdTime(DateUtils.parse(supportTime, DateUtils.DATE_TIME_PATTERN))
//                    .type(Integer.valueOf(value))
//                    .build();
//            supportDOList.add(supportDO);
//            //删除这条缓存
//            redissonHelper.deleteHash(RedisConstant.REDIS_KEY_ARTICLE_SUPPORT, key);
//        }
//        if (supportDOList.size() > 0) {
//            articleSupportMapper.batchInsert(supportDOList);
//            log.info("文章点赞记录更新成功,本次共更新记录{}条,{}", supportDOList.size(), supportDOList);
//        }
//        //更新点赞数
//        redissonHelper.scanGetStream(RedisConstant.REDIS_KEY_ARTICLE_SUPPORT_SUM).forEach(key -> {
//            Long blogId = Long.valueOf(key.split(":")[1]);
//            Long value = redissonHelper.getValueAto(key);
//            ArticleDataPO articleDataPO = ArticleDataPO.builder()
//                    .articleId(blogId)
//                    .supportAmount(value)
//                    .build();
//            articleDataPOList.add(articleDataPO);
//        });
//        if (articleDataPOList.size() > 0) {
//            articleDataMapper.updateBatchByBlogId(articleDataPOList);
//            log.info("文章点赞数更新成功,本次共更新文章{}条,{}", articleDataPOList.size(), articleDataPOList);
//        }
//        log.info("消费文章点赞缓存定时任务成功结束");
//    }
//
//
//}
